package com.tracesystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.commons.annotation.RuleValidate;
import com.tracesystem.controller.form.*;
import com.tracesystem.controller.info.SimpleUserInfo;
import com.tracesystem.model.IssueUser;
import com.tracesystem.service.IssueUserService;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.IssueUserException;
import com.tracesystem.util.exception.code.IssueUserExceptionCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 熊亮
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/issueUser")
@Api(tags = "用户操作相关接口 如登录、更新、新增")
public class IssueUserController {
    /**
     * 账号业务层对象
     */
    @Resource
    private IssueUserService issueUserService;

    /**
     * 添加新账号
     * @param insertUserForm
     * @return
     */
    @RuleValidate
    @ApiOperation("新增用户")
    @PostMapping("/insertIssueUser")
    public Result addIssueUser(@RequestBody InsertUserForm insertUserForm){
        return issueUserService.saveUser(insertUserForm);

    }

    /**
     * 修改账号资料
     * @param updateUserForm
     * @return
     */
    @RuleValidate
    @ApiOperation("更新用户")
    @PostMapping("/updateIssueUser")
    public Result updateIssueUser(@RequestBody UpdateUserForm updateUserForm){
        issueUserService.updateIssueUser(updateUserForm);
        return null;
    }

    /**
     * 为swegger提供的登录接口
     * @author  Kevin Martin
     * @param loginUserForm
     * @return
     */
    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result<?> login(@Validated LoginUserForm loginUserForm){
        return null;
    }

    /**
     * 分页获取用户可以进行通过用户名模糊查询
     * @author  Kevin Martin
     * @param getUserPageForm
     * @return
     */
    @RuleValidate
    @GetMapping("getUserPage")
    @ApiOperation("分页查询接口（可通过用户名模糊查询）")
    public Result<Page<SimpleUserInfo>> getUserPage(@Validated GetUserPageForm getUserPageForm){
        return  issueUserService.findUserPage(getUserPageForm);
    }

    /**
     * @return
     * @author Kevin Martin
     * 处理退出业务
     * 没有登录返回状态码403
     */
    @GetMapping("loginerror")
    public Result errotPage() {
        return Result.getInstance(String.class).setMsg("没有登录").setCode(403);

    }

    /**
     * 修改密码
     *
     * @param changePasswordForm
     * @return
     */
//    @RuleValidate
    @ApiOperation("修改密码")
    @PostMapping("changPassword")
    public Result changPassword(@Validated @RequestBody ChangePasswordForm changePasswordForm) {
        return issueUserService.ReWitePassword(changePasswordForm);
    }

    /**
     * 通过id删除用户
     *
     * @param rmUserForm
     * @return
     */
    @RuleValidate
    @ApiOperation("删除用户")
    @PostMapping("rmuser")
    public Result<?> rmUser(@Validated @RequestBody RmUserForm rmUserForm) {
        return issueUserService.cutUser(rmUserForm);
    }

}

